package com.example.todotask.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.example.todotask.R;
import com.example.todotask.adapter.TaskListAdapter;
import com.example.todotask.database.DatabaseHandler;
import com.example.todotask.database.TaskHandler;
import com.example.todotask.model.Task;

public class MainActivity extends Activity {

	//
	// Khoi tao cac thanh phan UI
	//
	// ---- ListView ----
	private ListView lvTasks;
	
	private DatabaseHandler db;
	private TaskHandler taskHandler;
	private List<Task> tasks;
	private TaskListAdapter taskListAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// khoi tao database
		db = new DatabaseHandler(this);
		taskHandler = new TaskHandler(db.getTaskDao());
		tasks = taskHandler.loadAllTask();
		
		// khoi tao UI element
		lvTasks = (ListView) findViewById(R.id.list_task);
		
		// set gia tri vao list view task
		taskListAdapter = new TaskListAdapter(this, R.layout.item_list_task, tasks);
		taskListAdapter.addAll(tasks);
		lvTasks.setAdapter(taskListAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/**
	 * Các sự kiện cho menu 
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_add_task: // New
			Intent intent = new Intent(this, AddTaskActivity.class);
			startActivity(intent);
			return true;
		case R.id.menu_item_delete_task: // Delete
			
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
